<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 成绩数据
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!-- <el-button
                    type="primary"
                    icon="el-icon-lx-add"
                    class="handle-del mr10"
                    @click="handleAdd"
                >添加</el-button> -->
                <!-- <el-input v-model="query.id" placeholder="id" class="handle-input mr10"></el-input> -->
                <el-input v-model="query.studentId" placeholder="学号" class="handle-input mr10"></el-input>
                <el-input v-model="query.studentName" placeholder="姓名" class="handle-input mr10"></el-input>
                <el-input v-model="query.contestName" placeholder="竞赛名称" class="handle-input mr10"></el-input>
                <el-input v-model="query.times" placeholder="答题次数" class="handle-input mr10"></el-input>
                
                <el-input v-model="query.time" placeholder="用时" class="handle-input mr10"></el-input>
                <el-input v-model="query.score" placeholder="得分" class="handle-input mr10"></el-input>
                <el-button type="primary" style="margin-right: 5px;" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <download-excel
                    class="export-excel-wrapper"
                    :data="multipleSelection"
                    :fields="json_fields"
                    :header="title"
                    name="用户成绩数据.xls"
                    >
                    <el-button type="primary" style="margin-right: 5px;" icon="el-icon-lx-down" @click="handleExport">导出选中</el-button>
                </download-excel>
                <download-excel
                    class="export-excel-wrapper"
                    :data="allData"
                    :fields="json_fields"
                    :header="title"
                    name="用户成绩数据(全部).xls"
                    >
                    <el-button type="primary" icon="el-icon-lx-down" @click="handleExportAll">全部导出</el-button>
                </download-excel>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <!-- <el-table-column prop="id" label="id"></el-table-column> -->
                <el-table-column prop="studentId" label="学号"></el-table-column>
                <el-table-column prop="studentName" label="姓名"></el-table-column>
                <el-table-column prop="contestName" label="竞赛名称"></el-table-column>
                <el-table-column prop="times" label="答题次数"></el-table-column>
                <el-table-column prop="time" label="用时(S)"></el-table-column>
                <el-table-column prop="score" label="得分"></el-table-column>

                <el-table-column prop="createTime" label="参赛时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.current"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                
                <el-form-item label="用时">
                    <el-input v-model="form.time"></el-input>
                </el-form-item>
                <el-form-item label="得分">
                    <el-input v-model="form.score"></el-input>
                </el-form-item>
                <el-form-item label="答题次数">
                    <el-input v-model="form.times"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 添加弹出框 -->
        <el-dialog title="添加成绩" :visible.sync="addVisible" width="30%">
            <el-form ref="addform" :model="addform" label-width="70px">
                <el-form-item label="学号">
                    <el-input v-model="addform.studentId"></el-input>
                </el-form-item>
                <el-form-item label="竞赛id">
                    <el-input v-model="addform.contestId"></el-input>
                </el-form-item>
                <el-form-item label="用时">
                    <el-input v-model="addform.time"></el-input>
                </el-form-item>
                <el-form-item label="得分">
                    <el-input v-model="addform.score"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getUserScoreList, deleteUserScore, addUserScore, updateUserScore,getAllUserScoreList } from '../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                current: 1,
                pageSize: 10,
            },
            tableData: [],
            multipleSelection: [],
            allData: [],
            delList: [],
            editVisible: false,
            addVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            title:'用户成绩数据',
            json_fields: {
                'id': 'id',
                '学号': 'studentId',
                '姓名': 'studentName',
                '竞赛名称': 'contestName',
                '竞赛Id': 'contestId',
                '答题次数': 'times',
                '用时': 'time',
                '得分': 'score',
                '参赛时间': 'createTime',
                '更新时间': 'updateTime'
            },

            addform: {
                "contestId": "",
                "score": "",
                "studentId": "",
                "time": ""
            }
        };
    },
    created() {
        this.getData();
        getAllUserScoreList().then(res => {
            this.allData=res.data
            console.log(this.allData)
        }).catch(err => {
            this.$message.error('加载用户成绩数据失败！')
        })
    },
    methods: {
        getData() {
            getUserScoreList(this.query).then(response => {
                if(response.code != 0){
                    this.$message.error(response.message)
                }else{
                    let res = response.data
                    this.tableData = res.records;
                    this.pageTotal = res.total;
                }
            });
            
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'current', 1);
            this.getData();
        },
        //导出表格
        handleExport() {
            if(this.multipleSelection.length==0){
                this.$message.error('请选中要导出的用户成绩数据！')
            }else{
                this.$message.success('导出成功！')
            }
        },
        handleExportAll() {
            this.$message.success('导出成功！')
        },
        //添加成绩
        saveAdd() {
            addUserScore(this.addform).then(res => {
                console.log(res)
                if(res.code!=0){
                    this.$message.error(res.message)
                }else{
                    this.$message.success('添加成功!')
                    this.addVisible=false
                    this.getData()
                }
            }).catch(err => {
                this.$message.error('添加失败，请检查信息是否正确!')
            })
        },
        handleAdd() {
            this.addVisible=true
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    deleteUserScore(row).then( res => {
                        if(res.code!=0){
                            this.$message.error(res.message)
                        }else{
                            this.$message.success('删除成功');
                            this.tableData.splice(index, 1);
                            this.pageTotal-=1
                        }
                    }).then(()=>{
                        
                    }).catch(err => {
                        this.$message.error('删除失败！')
                    })
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.form.gender=this.form.gender+''
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            updateUserScore(this.form).then(res => {
                if(res.code!=0){
                    this.$message.error(res.message)
                    this.getData()
                }else{
                    this.$message.success(`修改第 ${this.idx + 1} 行成功!`);
                    this.$set(this.tableData, this.idx, this.form);
                }
                
            }).catch(err => {
                this.$message.error('修改第 ${this.idx + 1} 行失败!')
            })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'current', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    display: flex;
}

.handle-select {
    width: 120px;
}

.formSelect {
    width: 100%;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
